package heman.server.controllers;

import heman.server.entities.Table;
import heman.server.entities.TableManager;



import ks.framework.common.Message;
import ks.framework.communicator.Communicator;
import ks.server.interfaces.IProcessServerMessage;


import org.w3c.dom.NamedNodeMap;


/**
 * Turn controller handles TURN messages.
 * 
 * Example:
 * 
 * <turn table="99" skip="false" move="(5/5)=L,(5/7)=E,(5/6)=I" complete="false">
  <gameState player="1284" score="35" game="special=value,client=only" /> 
  <gameState player="335" score="135" game="special=value,client=only" /> 
  </turn>

 * 
 * @author Wenlong Liu
 *
 */
public class TurnController implements IProcessServerMessage {

	public boolean process(Communicator com, Message m) {

     
        
        //Get all the attributes of <turn> node
        NamedNodeMap map=m.contents().getAttributes();

        
        String tableID=map.
        		getNamedItem("table").getNodeValue();
        Table table=TableManager.getInstance().getTable(tableID);

        if (table!=null){
        	//send it to all players on that table
        	boolean b=false;
        	b=new GameResponseController().process(com, m);
            //If the game ends, it is processed by an EndController
            if (Boolean.parseBoolean(map.getNamedItem("complete").getNodeValue()))
            		b=b && (new GameOverController().process(com,m));
            
        	//ALSO Send the game response
        	return b;
        }else {
			return false;
		}
        
        
        
        

	}

}
